//
//  SPAddFriendsViewController.m
//  Sport
//
//  Created by Cass on 2017/11/28.
//  Copyright © 2017年 Cass. All rights reserved.
//

#import "SPAddFriendsViewController.h"
#import <ContactsUI/ContactsUI.h>

@interface SPAddFriendsViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,CNContactPickerDelegate,EMContactManagerDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableVIew;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (nonatomic, strong) CNContactPickerViewController *contactPickerVC;
@end

@implementation SPAddFriendsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self approveRequest];
//    [self getRequest];
}

- (void)getRequest{
    [[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];
    
}
- (void)approveRequest{
    [[EMClient sharedClient].contactManager approveFriendRequestFromUser:@"13629049722"
                                                              completion:^(NSString *aUsername, EMError *aError) {
                                                                  if (!aError) {
                                                                      NSLog(@"同意好友成功");
                                                                  }
                                                              }];
}
- (void)friendRequestDidReceiveFromUser:(NSString *)aUsername message:(NSString *)aMessage{
    if (!aUsername) {
        return ;
    }
    if (!aMessage) {
        aMessage = [NSString stringWithFormat:@"用户:%@正在请求添加你",aUsername];
    }
    NSLog(@"成功接收到来自用户%@的请求，消息:%@",aUsername,aMessage);
}
- (void)getContactInfo{
    //创建选择联系人导航控制器
    _contactPickerVC = [[CNContactPickerViewController alloc]init];
    _contactPickerVC.delegate = self;
    [self presentViewController:_contactPickerVC animated:YES completion:nil];
    
}
- (void)isGetContacts{
    //获取通讯录的授权状态
    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    if (status == CNAuthorizationStatusNotDetermined) {
        CNContactStore *store = [CNContactStore new];
        [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
                NSLog(@"授权成功");
            }else{
                NSLog(@"授权失败");
            }
        }];
    }else if (status == CNAuthorizationStatusAuthorized){
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请去设置->隐私里授权！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            nil;
        }];
        [alertVC addAction:action];
        [self presentViewController:alertVC animated:YES completion:nil];
    }
}
#pragma mark - UISearchBarDelegate -
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    EMError *error = [[EMClient sharedClient].contactManager addContact:self.searchBar.text message:@"我想加你为好友"];
    if (!error) {
        NSLog(@"申请成功");
    }
    [self.view endEditing:YES];
}
#pragma mark - UITableViewDataSource -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        return 2;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"从通讯录获取";
        }
    }else if (indexPath.section == 1){
        nil;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        [self getContactInfo];
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10.0f;
}

//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//    [self.searchBar endEditing:YES];
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
